<?php


namespace api\controllers;


use api\components\BaseController;
use common\helpers\commonApi;
use common\helpers\PageApi;
use common\helpers\ValidateApi;
use common\models\Address;
use Yii;

/**
 * 地址控制器
 * Class AddressController
 * @package api\modules\v1\controllers
 */
class AddressController extends BaseController
{

    /**
     * 地址列表
     * @return array
     */
    public function actionList()
    {
        $userid = self::$userid;
        $page = (int)Yii::$app->request->get('page',1);
        $query = Address::find()
            ->where(['userid'=>$userid,'status'=>1])
            ->select(['address','id as address_id','id','campus_name','campus_id','sex','consignee','mobile'])
            ->orderBy(['create_time'=>SORT_DESC]);
        $data = PageApi::apiPage($query,$page);
        return commonApi::message('成功',true,$data);
    }

    /**
     * 添加地址
     * @return array
     */
    public function actionAdd()
    {
        $params = Yii::$app->request->post();
        $userid = self::$userid;
        $model = new Address();
        return $model->add($userid,$params);
    }

    /**
     * 修改地址
     * @return array
     */
    public function actionEdit()
    {
        $userid = self::$userid;
        $params = Yii::$app->request->post();
        $address_id = $params['address_id']??'';
        $model = new Address();
        return $model->edit($userid,$address_id,$params);
    }
}